// 加载大数据3dtitles配置
const maxDTilesOption = {
  clampToGround: false,
  skipLevelOfDetail: true,
  skipLevels: 1,
  skipScreenSpaceErrorFactor: 16,
  maximumScreenSpaceError: 10,
  cacheBytes: 1073741824,
  maximumCacheOverflowBytes: 2147483648,
  preferLeaves: true,
  immediatelyLoadDesiredLevelOfDetail: true,
  loadSiblings: true,
  progressiveResolutionHeightFraction: 0.8,//【重要】数
  dynamicScreenSpaceError: true,//true时会在真正的全屏加
  preloadWhenHidden: true,//tileset.show是false时，也去预
  cullRequestsWhileMovingMultiplier: 18,
  maximumMemoryUsage: 512,
};
// 默认模型配置
const defaultModelOption = {
  skipLevels: 1,
  skipScreenSpaceErrorFactor: 16,
  maximumScreenSpaceError: 10,
  cacheBytes: 268435456,
  maximumCacheOverflowBytes: 536870912,
  preferLeaves: true,
  immediatelyLoadDesiredLevelOfDetail: true,
  loadSiblings: true,
  progressiveResolutionHeightFraction: 0.8,//【重要dynamicScreenSpaceError:false,//true时会在真正的全
  preloadWhenHidden: true,//tileset.show是false时，也去
  cullRequestsWhileMovingMultiplier: 18,
  maximumMemoryUsage: 256,
}

export default class ModelAndImage {
  map3d: any;
  xyzLayer: any; //添加影像图层
  tiles3dLayer: any[]; //加载3dTiles

  constructor(map3d) {
    this.map3d = map3d;
    this.tiles3dLayer = [];
  }

  // 添加影像图层
  addXyzLayer(options: any) {
    const params: any = {
      type: "xyz", // 类型
      ...options
    };
    this.xyzLayer = new window.mars3d.layer.XyzLayer(params);
    this.map3d.addLayer(this.xyzLayer);
  }

  // 添加3DTile模型
  addCesium3DTileSet(url) {
    const dTilesOption = url.indexOf("HD_ALL") >= 0 || url.indexOf('TYQ_09_25') >= 0 ? maxDTilesOption : defaultModelOption;
    const tiles3dLayers = new window.mars3d.layer.TilesetLayer({
      url,
      ...dTilesOption,
      enableDebugWireframe: false, // 是否可以进行三角网的切换显示
      flyTo: false,
      position: {
        alt: 45
      }
    });
    this.map3d.addLayer(tiles3dLayers);
    this.tiles3dLayer.push(tiles3dLayers);
  }

  // 删除所有模型
  delete3DTile() {
    if (this.tiles3dLayer.length > 0) {
      this.tiles3dLayer.forEach((item) => {
        this.map3d.removeLayer(item, true);
      })
    }
  }
}
